package com.abc.scholarship.service.impl;

import com.abc.scholarship.common.enums.StatusEnum;
import com.abc.scholarship.domain.entity.Clazz;
import com.abc.scholarship.domain.entity.College;
import com.abc.scholarship.domain.entity.Major;
import com.abc.scholarship.exception.BusinessException;
import com.abc.scholarship.mapper.CollegeMapper;
import com.abc.scholarship.service.CollegeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 学院表 服务实现类
 * </p>
 *
 * @author jzw
 * @since 2024-07-02
 */
@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeMapper, College> implements CollegeService {

    @Autowired
    private CollegeMapper collegeMapper;

    @Override
    public List<College> getNotExitByNames(List<String> names) {
        Set<String> collect = collegeMapper.selectList(new LambdaQueryWrapper<College>()
                .select(College::getCollegeName)
                .in(College::getCollegeName, names)
        ).stream().map(College::getCollegeName).collect(Collectors.toSet());

        return new ArrayList<>(
                names.stream()
                        .filter(item -> !collect.contains(item))
                        .map(College::new)
                        .collect(Collectors.toSet())
        );
    }

    @Override
    public void setStatusByIds(List<Long> idList, Integer status) {
        collegeMapper.setStatusByIds(idList,status);
    }

    @Override
    public void MUpdateById(College college) {

        if(college.getStatus() == null){
            collegeMapper.updateById(college);
            return;
        }
        if(college.getStatus().equals(StatusEnum.FROZEN.getValue())){
            college.setStatus(StatusEnum.NORMAL.getValue());
        }else if(college.getStatus().equals(StatusEnum.NORMAL.getValue())){
            college.setStatus(StatusEnum.FROZEN.getValue());
        }else throw new BusinessException("状态异常，请联系管理员");
        collegeMapper.updateById(college);
    }
}
